#include "Substituter.h"
#include <string>
#include <iostream>
#include <map>
#include "./dicionario/Timer.h"

using namespace std;

Substituter::Substituter() {
//	charMap["&ndash;"] = '-';
//	charMap["&#8211;"] = '-';
//	charMap["&mdash;"] = '�';
//	charMap["&#8212;"] = '�';
//	charMap["&nbsp;"] = ' ';
//	charMap["&#160;"] = ' ';
//	charMap["&amp;"] = '&';
//	charMap["&#38;"] = '&';
//	charMap["&gt;"] = '>';
//	charMap["&#62;"] = '>';
//	charMap["&lt;"] = '<';
//	charMap["&#60;"] = '<';
	charMap["&aacute;"] = 'a';
	charMap["á"] = 'a';
	charMap["&#225;"] = 'a';
	charMap["&#193;"] = 'a';
	charMap["&agrave;"] = 'a';
	charMap["à"] = 'a';
	charMap["&#224;"] = 'a';
	charMap["&#192;"] = 'a';
	charMap["&acirc;"] = 'a';
	charMap["ã"] = 'a';
	charMap["&#226;"] = 'a';
	charMap["&#194;"] = 'a';
	charMap["&aring;"] = 'a';
	charMap["&#229;"] = 'a';
	charMap["&#197;"] = 'a';
	charMap["&atilde;"] = 'a';
	charMap["ã"] = 'a';
	charMap["&#227;"] = 'a';
	charMap["&#195;"] = 'a';
	charMap["&auml;"] = 'a';
	charMap["ä"] = 'a';
	charMap["&#228;"] = 'a';
	charMap["&#196;"] = 'a';
	charMap["&ccedil;"] = 'c';
	charMap["ç"] = 'c';
	charMap["&#231;"] = 'c';
	charMap["&#199;"] = 'c';
	charMap["&eacute;"] = 'e';
	charMap["é"] = 'e';
	charMap["&#233;"] = 'e';
	charMap["&#201;"] = 'e';
	charMap["&egrave;"] = 'e';
	charMap["è"] = 'e';
	charMap["&#232;"] = 'e';
	charMap["&#200;"] = 'e';
	charMap["&ecirc;"] = 'e';
	charMap["ê"] = 'e';
	charMap["&#234;"] = 'e';
	charMap["&#202;"] = 'e';
	charMap["&euml;"] = 'e';
	charMap["ë"] = 'e';
	charMap["&#235;"] = 'e';
	charMap["&#203;"] = 'e';
	charMap["&iacute;"] = 'i';
	charMap["í"] = 'i';
	charMap["&#237;"] = 'i';
	charMap["&#205;"] = 'i';
	charMap["&igrave;"] = 'i';
	charMap["ì"] = 'i';
	charMap["&#236;"] = 'i';
	charMap["&#204;"] = 'i';
	charMap["&icirc;"] = 'i';
	charMap["î"] = 'i';
	charMap["&#238;"] = 'i';
	charMap["&#206;"] = 'i';
	charMap["&iuml;"] = 'i';
	charMap["ï"] = 'i';
	charMap["&#239;"] = 'i';
	charMap["&#207;"] = 'i';
	charMap["&ntilde;"] = 'n';
	charMap["ñ"] = 'n';
	charMap["&#241;"] = 'n';
	charMap["&#209;"] = 'n';
	charMap["&oacute;"] = 'o';
	charMap["ó"] = 'o';
	charMap["&#243;"] = 'o';
	charMap["&#211;"] = 'o';
	charMap["&ograve;"] = 'o';
	charMap["ò"] = 'o';
	charMap["&#242;"] = 'o';
	charMap["&#210;"] = 'o';
	charMap["&ocirc;"] = 'o';
	charMap["ô"] = 'o';
	charMap["&#244;"] = 'o';
	charMap["&#212;"] = 'o';
	charMap["&otilde;"] = 'o';
	charMap["õ"] = 'o';
	charMap["&#245;"] = 'o';
	charMap["&#213;"] = 'o';
	charMap["&ouml;"] = 'o';
	charMap["ö"] = 'o';
	charMap["&#246;"] = 'o';
	charMap["&#214;"] = 'o';
	charMap["&uacute;"] = 'u';
	charMap["ú"] = 'u';
	charMap["&#250;"] = 'u';
	charMap["&#218;"] = 'u';
	charMap["&ugrave;"] = 'u';
	charMap["ù"] = 'u';
	charMap["&#249;"] = 'u';
	charMap["&#217;"] = 'u';
	charMap["&ucirc;"] = 'u';
	charMap["û"] = 'u';
	charMap["&#251;"] = 'u';
	charMap["&#219;"] = 'u';
	charMap["&uuml;"] = 'u';
	charMap["ü"] = 'u';
	charMap["&#252;"] = 'u';
	charMap["&#220;"] = 'y';
	charMap["&yuml;"] = 'y';
	charMap["y"] = 'y';
	charMap["&#255;"] = 'y';
}

char Substituter::getEquivalentChar(string specialHtml) {
	if (charMap[specialHtml] != 0)
		return charMap[specialHtml];
	else
		return ' ';
}

string Substituter::convertHtmlString(const string & str, double & t) {
	Timer timer;
	timer.begin();
	uint eComPos;
	char c;
	string returnString = "";
//	cout << str;
	returnString.reserve(str.size());
	int semiColPos;
	for (uint i = 0; i < str.size(); i++) {
		if (!isalnum(str[i])) {
			if ( (c = str[i]) == '&') {
				semiColPos = str.find(';', i);
//				cout << "i:  " << i << endl;
//				cout << "semiColPos: " << semiColPos << endl;
				if (semiColPos - i < 9) {
					char aux = getEquivalentChar(str.substr(i, semiColPos - i + 1));
//					cout << "blah " << aux << endl;
					returnString += aux;
					i = semiColPos;
				}
			}
			else {
//				cout << "blah1: " << str[i] << endl;
				char aux2 = getEquivalentChar(str.substr(i, 1));
//				cout << "blah2: " << aux2 << endl;
				returnString += aux2;
			}
		}
		else 
			returnString += str[i];
	}
	timer.end();
	t += timer.diff();
	return returnString;
}
